package com.cxyxj.importdemo;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.*;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.filter.AnnotationTypeFilter;

import java.util.Map;


/**
 * @author: gongj
 * @create: 2022-01-25 15:40
 */
public class ImportBeanDefinitionRegistrarTest implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        // 获得 ComponentScan 注解
        Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(ComponentScan.class.getName());
        // 获得 basePackages 的信息
        String[] basePackages = (String[]) annotationAttributes.get("basePackages");
        // 扫描器，这也是Spring底层扫描使用的
        ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry, false);
        // 满足任意 includeFilters的会被扫描
        scanner.addIncludeFilter(new AnnotationTypeFilter(Cxyxj.class));
        // 继续扫描
        scanner.scan(basePackages);
    }

}
